﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace HQWeb.SysManage.RoleManage
{
    public partial class MenuAddRole : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            this.LeftTree.Attributes.Add("onclick", "CheckEvent()");//绑定事件
            if (!Page.IsPostBack)
            {
                try
                {
                    // operRecode = new COM.HS.ErrorLog.OperationRecode();
                    InitTree();
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        #region 递归绑定同一个表数据
        #region 显示一级菜单（系统菜单）
        private void InitTree()
        {
            BLL.SysManage.T_PUB_MENU menulBll = new BLL.SysManage.T_PUB_MENU();

            List<Model.SysManage.T_PUB_MENU> TreeList = menulBll.GetnextMenu("0", "");

            foreach (Model.SysManage.T_PUB_MENU TreeModel in TreeList)
            {
                TreeNode node = new TreeNode();
                node.Text = TreeModel.MENU_NAME.Trim();
                node.Value = TreeModel.MENU_ID.ToString();
                if (CheckEdNodesTrue(TreeModel.MENU_ID.ToString()))
                {
                    node.ShowCheckBox = false;//当表中有此数据时不显示复选框
                }
                node.Expanded = true;
                LeftTree.Nodes.Add(node);
                AddReplies(node, TreeList);
            }
        }
        #endregion

        #region 递归绑定系统菜单下的菜单
        private void AddReplies(TreeNode node, IList newList)
        {
          
            BLL.SysManage.T_PUB_MENU menulBll = new BLL.SysManage.T_PUB_MENU();
            Model.SysManage.T_PUB_MENU systemTreeModel = new Model.SysManage.T_PUB_MENU();

            List<Model.SysManage.T_PUB_MENU> TreeList = menulBll.GetnextMenu(node.Value,"");

            foreach (Model.SysManage.T_PUB_MENU TreeModel in TreeList)
            {
                TreeNode replyNode = new TreeNode();
                replyNode.Text = TreeModel.MENU_NAME.Trim();
                replyNode.Value = TreeModel.MENU_ID.ToString();
                if (CheckEdNodesTrue(TreeModel.MENU_ID.ToString()))
                {
                    replyNode.ShowCheckBox = false;//当表中有此数据时不显示复选框
                }
                replyNode.Expanded = false;
                node.ChildNodes.Add(replyNode);
                AddReplies(replyNode, TreeList);
            }
        }
        #endregion


        #endregion
        #region 判断SYSTEM_TRE_ROLE表中是否有包括SYSTEM_TREE的数据，当有时返回为TRUE
        /// <summary>
        /// 判断SYSTEM_TRE_ROLE表中是否有包括SYSTEM_TREE的数据，当有时返回为TRUE
        /// </summary>
        /// <param name="vNodeValue">树型菜单ID</param>
        /// <returns></returns>
        private bool CheckEdNodesTrue(string vNodeValue)
        {
            BLL.SysManage.T_PUB_ROLEMENU treeRoleBll = new BLL.SysManage.T_PUB_ROLEMENU();
            List<Model.SysManage.T_PUB_ROLEMENU> TreeRoleList = treeRoleBll.GetListByRole(Session["idSign"].ToString());

            foreach (Model.SysManage.T_PUB_ROLEMENU treeRoleModel in TreeRoleList)
            {
                if (vNodeValue == Convert.ToString(treeRoleModel.MENU_ID))
                {
                    return true;
                }
            }
            return false;
        }
        #endregion
        #region 点击保存后响应事件，把页面勾选的信息保存到数据库中
        /// <summary>
        /// 点击保存后响应事件，把页面勾选的信息保存到数据库中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btSave_Click(object sender, EventArgs e)
        {
            try
            {
                BLL.SysManage.T_PUB_ROLEMENU treeRoleBll = new BLL.SysManage.T_PUB_ROLEMENU();
              
                if (LeftTree.CheckedNodes.Count > 0)
                {
                    foreach (TreeNode node in LeftTree.CheckedNodes)
                    {
                        if (node.Value != "0")
                        {
                            Model.SysManage.T_PUB_ROLEMENU treeRoleModel = new Model.SysManage.T_PUB_ROLEMENU();
                            treeRoleModel.MENU_ID = Convert.ToInt32(node.Value);
                            treeRoleModel.ROLE_ID = Convert.ToInt32(Session["idSign"].ToString());
                            if (!CheckEdNodesTrue(node.Value))
                            {
                               // operRecode.setOperation("增加", "SYSTEM_TREE_ROLE_SQL", "角色的菜单权限");
                                treeRoleBll.Add(treeRoleModel);
                                //logBll.AddLoginInfo(Convert.ToInt32(treeRoleModel.ID), "增加", "角色的菜单权限", "成功", "", "", Request.Path);
                               // operRecode.operateSucess();
                            }
                        }
                    }
                    Universal.JS.RefreshParentData();
                    Universal.JS.CloseWindow();
                }
                else
                {
                    Response.Write("<script language='javascript'> alert('没有选择！');</script>");
                    this.sign.Text = null;
                    this.Select_signID.Text = null;
                }
            }
            catch (Exception ex)
            {
                Universal.JS.Alert(ex.Message);
                return;
            }
        }
        #endregion
    }

}